package cn.b.sky.mes.pp.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.b.sky.mes.pp.model.ProcessPlanRate;
import cn.b.sky.mes.pp.model.condition.ProcessPlanRateCondition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.file.ExcelUtil;
import cn.b.sky.common.utils.DateUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.mes.pp.constants.ProductConstant;
import cn.b.sky.mes.pp.enums.OperationTaskStatus;
import cn.b.sky.mes.pp.service.OperationTaskService;
import cn.b.sky.mes.pp.service.OrderTypeService;
import cn.b.sky.mes.pp.service.ProcessPlanRateService;

/**
 * 
 * <strong>Title : ProcessPlanRateController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.pp.controller<br></strong>
 * <strong>Description : 工序任务计划达成率报表控制器</strong><br> 
 * <strong>Create on : 2016-8-8 上午10:33:19<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("operationplanRate")
@Slf4j
public class ProcessPlanRateController extends SecurityController {
  
  @Resource
  OrderTypeService orderTypeService;
  
  @Resource
  ProcessPlanRateService processPlanRateService;
  @Resource
  OperationTaskService operationTaskService;

  /**
   * 
   * <strong>Title : operationPlanRateView<br></strong>
   * <strong>Description : 计划达成率报表</strong><br> 
   * <strong>Create on : 2016-8-8 上午10:29:46<br></strong>
   * <p>
   * @param navTabId
   * @param condition
   * @param model
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   * @throws ParseException 
   */
  @RequestMapping("report")
  public String processPlanRateReport(String navTabId, Model model, HttpSession session,
      @ModelAttribute("condition") ProcessPlanRateCondition condition) throws ParseException {
    condition.setSiteCode(this.getSiteCode());
    String now = DateUtils.getDateUtils(DateUtils.YYYY_MM_DD).getDateStr(new Date()); //今天日期字符串
    if(StringUtils.isBlank(condition.getMinPlannedStartDateTime())){
      condition.setMinPlannedStartDateTime(DateUtils.getDateUtils(DateUtils.YYYY_MM_DD).operationDay(now, -7));
      condition.setMaxPlannedStartDateTime(now);
    }
    if(StringUtils.isBlank(condition.getMinPlannedEndDateTime())){
      condition.setMinPlannedEndDateTime(now);
      condition.setMaxPlannedEndDateTime(now);
    }
    condition.setStatusEcode(OperationTaskStatus.CREATE.getValue());
    //保存sessionCondition
    ProcessPlanRateCondition sessionCondition = new ProcessPlanRateCondition();
    BeanUtils.copyProperties(condition, sessionCondition);
    session.setAttribute(ProductConstant.PROCESSPLANRATE_CONDITION, sessionCondition);
    //订单类型
    model.addAttribute("orderTypes", orderTypeService.getAllUnlockedOrderTypes(this.getSiteCode()));
    List<ProcessPlanRate> retes = processPlanRateService.getProcessPlanRateListByCondition(condition);
    int count = processPlanRateService.getProcessPlanRateCountByCondition(condition);
    condition.setData(retes);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "pp/operationTask/operationPlan_rate";
  }
  
  /**
   * 
   * <strong>Title : processPlanRateView<br></strong>
   * <strong>Description : 详情</strong><br> 
   * <strong>Create on : 2016-8-8 上午11:11:53<br></strong>
   * <p>
   * @return
   * String
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping("view")
  public String processPlanRateView(String navTabId, Model model, HttpSession session,
      @ModelAttribute("condition")ProcessPlanRateCondition condition) {
    
    ProcessPlanRateCondition newCondition = getDetailsCondition(session);
    //详情页面的参数从新赋值
    newCondition.setOperationCode(condition.getOperationCode());
    newCondition.setNumPerPage(condition.getNumPerPage());
    newCondition.setPageNum(condition.getPageNum());
    newCondition.setOrderDirection(condition.getOrderDirection());
    newCondition.setOrderField(condition.getOrderField());
    List<ProcessPlanRate> rates = processPlanRateService.getProcessPlanRateDetails(newCondition);
    int count = processPlanRateService.getgetProcessPlanRateDetailsCount(newCondition);
    condition.setData(rates);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "pp/operationTask/operationplan_view";
  }

  /**
   * 
   * <strong>Title : getDetailsCondition<br></strong>
   * <strong>Description : 获取详情页面的查询condition，防止改变session里的conditon</strong><br> 
   * <strong>Create on : 2016-8-9 下午3:34:47<br></strong>
   * <p>
   * @param session
   * @return
   * ProcessPlanRateCondition
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  private ProcessPlanRateCondition getDetailsCondition(HttpSession session) {
    ProcessPlanRateCondition newCondition = new ProcessPlanRateCondition();
    if (session.getAttribute(ProductConstant.PROCESSPLANRATE_CONDITION) != null) {
      ProcessPlanRateCondition sessionCondition = (ProcessPlanRateCondition) session.getAttribute(ProductConstant.PROCESSPLANRATE_CONDITION);
      if(null != sessionCondition){     
        //把condition的属性copy到sessionCondition中，详情要带上列表的查询条件，并且带上工序code和详情分页信息。
        BeanUtils.copyProperties(sessionCondition, newCondition);
      }
    }
    newCondition.setSiteCode(this.getSiteCode());
    return newCondition;
  }
  
  @RequestMapping(value = "exportToExcel")
  @ResponseBody
  public void exportProcessPlanRatesToExcel(HttpSession session,HttpServletResponse response, 
      String tableMap, String tableId) throws IOException {
    
    ProcessPlanRateCondition condition = null;
    if (session.getAttribute(ProductConstant.PROCESSPLANRATE_CONDITION) != null) {
      condition = (ProcessPlanRateCondition) session.getAttribute(ProductConstant.PROCESSPLANRATE_CONDITION);
    }
    if(null == condition){
      condition =  new ProcessPlanRateCondition();
    }
    condition.setSiteCode(this.getSiteCode());
    condition.setPageNum(1);
    condition.setEndRow(-1);
    List<ProcessPlanRate> retes = processPlanRateService.getProcessPlanRateListByCondition(condition);
    try {
      ExcelUtil.exportToExcel(response, getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), 
          "processPlanRate.C"), ExcelUtil.parseTableMapStr2TitleMap(tableMap, tableId), retes);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }
  
  /**
   * 
   * <strong>Title : exportProcessPlanRatesDetailToExcel<br></strong>
   * <strong>Description : </strong>导出详情列表，不考虑图表导出<br> 
   * <strong>Create on : 2016-8-10 下午2:50:30<br></strong>
   * <p>
   * @param session
   * @param response
   * @param tableMap
   * @param tableId
   * @param operationCode
   * @throws IOException
   * void
   * @throws 
   * @author lidapeng<br>
   * @version <strong>v1.0.0</strong><br>
   * <br>
   * <strong>修改历史:</strong><br>
   * 修改人 | 修改日期 | 修改描述<br>
   * -------------------------------------------<br>
   * <br>
   * <br>
   */
  @RequestMapping(value = "exportDetailToExcel")
  @ResponseBody
  public void exportProcessPlanRatesDetailToExcel(HttpSession session,HttpServletResponse response, 
      String tableMap, String tableId, String operationCode) throws IOException {
    
    ProcessPlanRateCondition condition = getDetailsCondition(session);
    condition.setOperationCode(operationCode);
    condition.setSiteCode(this.getSiteCode());
    condition.setPageNum(1);
    condition.setEndRow(-1);
    List<ProcessPlanRate> retes = processPlanRateService.getProcessPlanRateDetails(condition);
    try {
      ExcelUtil.exportToExcel(response, getImeViewMessageI18n(SecurityContextUtil.getLanguageCode(), 
          "processPlanRate.C"), ExcelUtil.parseTableMapStr2TitleMap(tableMap, tableId), retes);
    } catch (IOException e) {
      log.error("{}", e);
    }
  }
  
}
